package com.project.xy.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.project.common.utils.Serial;
import com.project.xy.entity.DFile;
import com.project.xy.entity.DModuleDetails;
import com.project.xy.entity.DesignModule;
import com.project.xy.mapper.DModuleDetailsMapper;
import com.project.xy.mapper.DesignModuleMapper;
import com.project.xy.service.DesignModuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.List;

@Service
@Transactional
public class DesignModuleImpl implements DesignModuleService {
    @Autowired
    private DesignModuleMapper designModuleMapper;
    @Autowired
    private DModuleDetailsMapper dModuleDetailsMapper;

    @Override
    public DesignModule queryByProductId(String productId) {
        return designModuleMapper.queryByProductId(productId);
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return designModuleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(DesignModule record) {
        return designModuleMapper.insert(record);
    }

    @Override
    public int insertSelective( DesignModule record, String details ,DFile dFile ) throws IOException {

        record.setFirstKindId(dFile.getFIRST_KIND_ID());
        record.setFirstKindName(dFile.getFIRST_KIND_NAME());
        record.setSecondKindId(dFile.getSECOND_KIND_ID());
        record.setSecondKindName(dFile.getSECOND_KIND_NAME());
        record.setThirdKindId(dFile.getTHIRD_KIND_ID());
        record.setThirdKindName(dFile.getTHIRD_KIND_NAME());

        Serial serial = new Serial();
        serial.addUser("DESIGN_ID","200");
        String designId = serial.getNumber("DESIGN_ID", "200", null, null, null);
        //把申请单编号 存入实体类中
        record.setDesignId(designId);
        //入库申请单 默认值给 S001-0 为等待审核
        record.setCheckTag("S001-0");
        record.setChangeTag("B002-0");
        //将json数据转成Java对象（SGatherDetails）
        DModuleDetails[] arr = JSONObject.parseObject(details, DModuleDetails[].class);
//        获取cost_price_sum
        Integer costPrice = 0;
        for(int i=0;i<arr.length;i++){
            DModuleDetails dModuleDetails = arr[i];
            Integer subtotal = dModuleDetails.getSubtotal();
            costPrice = costPrice + subtotal;
        }
        record.setCostPriceSum(costPrice);
        int i2 = designModuleMapper.insertSelective(record);

        //获取入库申请单（SGather）中的主键
        Integer id = record.getId();
        //循环存入数据库中
        int i1 = 0;
        for (int i=0;i<arr.length;i++){
            DModuleDetails dModuleDetails = arr[i];
            dModuleDetails.setParentId(id);
            dModuleDetails.setDetallsNumber(i+1);
            i1 = dModuleDetailsMapper.insertSelective(dModuleDetails);
        }
        if (i2>0&&i1>0){
            return 1;
        }else{
            return 0;
        }

    }

    @Override
    public DesignModule selectByPrimaryKey(Integer id) {
        return designModuleMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(DesignModule record) {
        return designModuleMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(DesignModule record) {
        return designModuleMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<DesignModule> selectByComposite(String state) {
        return designModuleMapper.selectByComposite(state);
    }

    @Override
    public DesignModule selectDesignByPid(String pid) {
        return designModuleMapper.selectDesignByPid(pid);
    }

    @Override
    public List<DesignModule> selectUpdMateriel() {
        return designModuleMapper.selectUpdMateriel();
    }

    @Override
    public List<DesignModule> selectList() {
        return designModuleMapper.selectList();
    }

    @Override
    public DesignModule selectByProdId(String id) {
        return designModuleMapper.selectByProdId(id);
    }
}
